package com.whp.yky.yky.controller;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.whp.yky.yky.entity.poentity.Data;

import com.whp.yky.yky.response.ResponseData;
import com.whp.yky.yky.service.DataService;

import com.whp.yky.yky.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "资料接口")
@CrossOrigin
@RestController
public class DataController {

    @Autowired
    private DataService dataService;


    @ApiOperation(value="获取资料信息", notes="根据用户名获取资料全部信息")
    @PostMapping("/findAllByDataName")
    public Object getDataInfoByID(@RequestParam String data_name){

        if (data_name!=null&&!"".equals(data_name)){

            return dataService.getDataInfoByDataName(data_name);

        }else{

            return "获取信息失败";

        }


    }

    @ApiOperation(value="上传资料", notes="上传资料")
    @PostMapping("/insertData")
    public String insertDataInfo(@RequestBody Map<String,String> dataMap){


        Data data=new Data();
        data.setId(dataMap.get("id"));
        data.setData_name(dataMap.get("data_name"));
        data.setData_type(dataMap.get("data_type"));
        data.setData_state(dataMap.get("data_state"));
        data.setPerson(dataMap.get("person"));
        data.setAdress(dataMap.get("adress"));

        boolean result= dataService.insertDataInfo(data);
        if (result){
            return "success";
        }else {
            return "fail";
        }
    }

    @DeleteMapping("/deletDataByID/{id}")
    @ApiOperation(value="删除资料", notes="根据id删除资料")
    public String deletDataByID(@PathVariable("id") String id){
        boolean result = dataService.deleteDataById(id);
        if (result){
            return "sucess";
        }else{
            return "false";
        }
    }

    @ApiOperation(value="修改资料信息", notes="修改资料信息")
    @PostMapping("/updateDataInfo")
    public ResponseData updateDataInfo(@RequestBody Map<String,String> dataMap){

        ResponseData res = new ResponseData();
        Data data=new Data();
        data.setId(dataMap.get("id"));
        data.setData_name(dataMap.get("data_name"));
        data.setData_type(dataMap.get("data_type"));
        data.setData_state(dataMap.get("data_state"));
        data.setPerson(dataMap.get("person"));
        data.setAdress(dataMap.get("adress"));
        res.setCode(20000);
        res.setData(dataService.updateDataInfo(data));
        return res;
    }

    @ApiOperation(value="获取所有资料信息", notes="获取所有资料信息")
    @GetMapping("/DataInfo")
    public ResponseData getDataInfo(){

        ResponseData res = new ResponseData();
//        try {
//            DecodedJWT verify = JwtUtil.verify(token);
//
//        }catch (SignatureVerificationException e){
//            e.printStackTrace();
//        }catch (TokenExpiredException e){
//            e.printStackTrace();
//        }catch (AlgorithmMismatchException e){
//            e.printStackTrace();
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        
        res.setCode(20000);
        res.setData(dataService.getDataInfo());
        res.setTotal(dataService.getDataInfo().size());


        return res;
    }

}
